//
//  UIImageView+Pop.m
//  MobileCRM
//
//  Created by winter on 16/3/2.
//  Copyright © 2016年 江苏金恒信息科技有限公司. All rights reserved.
//

#import "UIImageView+Pop.h"
#import "UIColor+Util.h"
@implementation UIImageView(Pop)

- (void)wsPopImageView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self convertRect:self.frame toView:[UIApplication sharedApplication].keyWindow]];
    imageView.image = self.image;
    imageView.userInteractionEnabled = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    [view addSubview:imageView];
    [view setBackgroundColor:[UIColor colorWithHexStringWithAlpha:@"#000000" alpha:.75]];
    //[view setAlpha:.9];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    [view addGestureRecognizer:tap];
    tap.delegate = self;
    [UIView animateWithDuration:.7 animations:^{
        CGRect frame = imageView.frame;
        frame.size.height = view.frame.size.width*imageView.image.size.height/imageView.image.size.width;
        frame.size.width = view.frame.size.width;
        imageView.frame = frame;
        imageView.center = view.center;
    }];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isKindOfClass:[UIImageView class]])
    {
        /// image full fill the screen
       if(touch.view.frame.size.height <= [UIScreen mainScreen].bounds.size.height*.9)
       {
           return NO;
       }
    }
    [gestureRecognizer.view removeFromSuperview];
    return NO;
}

- (void)addPopImageView:(BOOL)add
{
    if(add)
    {
        [self setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];
        [self addGestureRecognizer:tap];
    }
}
- (void)tapImage
{
    if(!self.image)return;
    [self wsPopImageView];
}
@end
